home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15353 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  72 lines

  1. Path: news.nas.com!news
  2. From: gldnspud@kali.nas.com (Matt Scott)
  3. Newsgroups: comp.lang.c++
  4. Subject: Functions in functions using function objects
  5. Date: Thu, 04 Apr 1996 23:59:46 GMT
  6. Organization: Everyone's Software
  7. Message-ID: <4k1nus$8h1@barad-dur.nas.com>
  8. NNTP-Posting-Host: kali.nas.com
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. I was experimenting with various C++ constructs lately, and I thought
  12. of an idea that might help me implement a Pascal construct that I
  13. really enjoy but that isn't present in C or C++.
  14.  
  15. I always liked the fact that Pascal was a block-structured language,
  16. and that you could have functions (and procedures, for all you Pascal
  17. types out there) inside other functions, which would be local to only
  18. those functions that they were defined in.
  19.  
  20. Since C and C++ are not block-structured, this is impossible. I did
  21. find a way around it, though:
  22.  
  23. #include <iostream.h>
  24.  
  25. void foo(void)
  26.     {
  27.     struct foo__bar
  28.         {
  29.         void operator()(void)
  30.             {
  31.             cout << "foobar!\n";
  32.             }
  33.         } bar;
  34.  
  35.     bar();
  36.     bar();
  37.     }
  38.  
  39. int main(void)
  40.     {
  41.     foo();
  42.     return 0;
  43.     }
  44.  
  45.  
  46. However, foo__bar::operator() is generated as an inline function. I
  47. would like to coax the language into giving me a non-inline function
  48. (possibly to implement recursion) using something similar to the
  49. above. This obviously cannot be implemented as:
  50.  
  51. void foo(void)
  52.     {
  53.     struct foo__bar
  54.         {
  55.         void operator()(void);
  56.         } bar;
  57.  
  58.     void foo__bar::operator()(void)
  59.         {
  60.         cout << "foobar!\n";
  61.         }
  62.  
  63.     bar();
  64.     bar();
  65.     }
  66.  
  67. Does anyone have any ideas on this subject?
  68.  
  69. -Matt
  70.  
  71.  
  72.